package routes

import (
	"mall/api/api"
	"mall/middleware"

	"github.com/gin-gonic/gin"
)

//路由配置
func NewRouter() *gin.Engine {
	//使用默认gin路由
	r := gin.Default()
	//加入日志中间件，跨域中间件
	r.Use(middleware.NewLogger(), middleware.Cors())
	v1 := r.Group("api/api")
	{
		//ping-pong
		v1.GET("ping", func(c *gin.Context) {
			c.JSON(200, "success")
		})

		//用户操作----------------DONE
		v1.POST("user/register", api.UserRegister) //用户注册
		v1.POST("user/login", api.UserLogin)       //用户登录

		//商品操作------------------DONE
		v1.GET("products", api.ListProducts)     //展示商品列表
		v1.GET("product/:id", api.ShowProduct)   //展示商品详情
		v1.POST("products", api.SearchProducts)  //搜索商品
		v1.GET("imgs/:id", api.ListProductImg)   //商品图片
		v1.GET("categories", api.ListCategories) //商品分类

		//轮播图---------------------------------------------undo
		v1.GET("carousels", api.ListCarousels) //轮播图

		authed := v1.Group("/") //需要登陆保护
		authed.Use(middleware.JWT())
		{

			// 用户操作---------------------------------------DONE
			authed.PUT("user", api.UserUpdate)               //用户更改昵称
			authed.POST("user/sending-email", api.SendEmail) //用户绑定邮箱及修改密码
			authed.POST("user/valid-email", api.ValidEmail)  //邮箱验证
			authed.POST("avatar", api.UploadAvatar)          //上传头像

			// 商品操作----------------------------------------DONE
			authed.POST("product", api.CreateProduct)       //添加商品
			authed.PUT("product/:id", api.UpdateProduct)    //更新商品
			authed.DELETE("product/:id", api.DeleteProduct) //删除商品

			// 收藏夹-----------------------------------------DONE
			authed.GET("favorites", api.ShowFavorites)         //展示收藏夹
			authed.POST("favorites", api.CreateFavorite)       //添加收藏
			authed.DELETE("favorites/:id", api.DeleteFavorite) //删除收藏

			// 订单操作-------------------------------------------DONE
			authed.POST("orders", api.CreateOrder)       //创建订单
			authed.GET("orders", api.ListOrders)         //展示订单
			authed.GET("orders/:id", api.ShowOrder)      //展示订单详情
			authed.DELETE("orders/:id", api.DeleteOrder) //删除订单

			//购物车------------------------------------------------DONE
			authed.POST("carts", api.CreateCart)       // 添加购物车
			authed.GET("carts", api.ShowCarts)         // 展示购物车
			authed.PUT("carts/:id", api.UpdateCart)    // 更新购物车
			authed.DELETE("carts/:id", api.DeleteCart) //删除

			//收获地址操作 ----------------------------------DONE
			authed.POST("addresses", api.CreateAddress)       //添加收获地址
			authed.GET("addresses", api.ShowAddresses)        //展示收货地址
			authed.PUT("addresses/:id", api.UpdateAddress)    //更新收货地址
			authed.DELETE("addresses/:id", api.DeleteAddress) //删除收获的之

			// 支付功能--------------------------------------DONE
			authed.POST("paydown", api.OrderPay)
			// authed.PATCH("paycart", api.CartPay)

			// 显示金额-----------------------------------------DONE
			authed.POST("money", api.ShowMoney)
		}
	}
	return r
}
